<?php
/**
 * 单例模式
 * Class Singleton.
 * Created by JiangLin 2016/11/8 16:12
 * E-mail jianglin@gxlm.com
 */

namespace App\Utils;

trait Singleton {
    static private $instance;

    /**
     * 创建此单例的新实例
     * @return static
     */
    final public static function instance() {
        return isset(static::$instance) ? static::$instance : static::$instance = new static;
    }

    /**
     * 构造函数
     * InstanceOnly constructor.
     */
    final protected function __construct() {
        $this->init();
    }

    // 从构造函数参数初始化单例
    protected function init(){}

    // 不允许复制对象
    public function __clone() {
        // TODO: Implement __clone() method.
        trigger_error('Cloning '.__CLASS__.' is not allowed.', E_USER_ERROR);
    }

    // 不允许序列化对象
    public function __wakeup() {
        // TODO: Implement __wakeup() method.
        trigger_error('Unserializing '.__CLASS__.' is not allowed.', E_USER_ERROR);
    }
}
